package com.tykj.yangningyu.chatdemo;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者：yangningyu on 2017/1/6 14:49
 * 邮箱：13315169475@163.com
 * xianxiliebiao
 */


public class MsgAdapter extends BaseAdapter {
    private List<String> mData = new ArrayList<>();

    public void setData(List<String> list) {
        this.mData = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mData != null ? mData.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return mData != null ? mData.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder ;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        String s = mData.get(position);
        holder.tv_msg.setText(s);
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.tv_msg)
        public TextView tv_msg;

        public ViewHolder(View convertView) {
            ButterKnife.bind(this, convertView);
        }
    }
}
